﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;

namespace ChatServer.model
{
    public class User : dbconn.DBConnector
    {
        private string _username;
        private string _password;

        //individual userSocket for sending and identification
        private UserSocket _userSocket;

        //private list of activeUsers for reference
        private List<User> _listActiveUsers;

        public string UserName
        {
            get { return _username; } //end get
            set { _username = value; }//end set
        }

        public string PassWord
        {
            get { return _password; } //end get
            set { _password = value; }//end set
        }

        public List<User> ListActiveUsers
        {
            get { return _listActiveUsers; }
        }

        public UserSocket UserSocket
        {
            get { return _userSocket; }
        }

        //constructor is used for requiring username and password. verification on empty strings are implemented
        public User(string userName, string password)
        {
            
            //verify that the parameters are not empty
            if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
            {
                //if strings are not empty, pass values to private variables
                _username = userName;
                _password = password;
            }
            //do nothing if the variables are empty. when authenticating through serverSocket, variables will be found
            //empty and return false
        }

            
        // Calling this function to authenticate user_name and password from database then return a value true or false
        public Boolean Authentication(string _username, string _password)
        {
            List<SqlParameter> login = new List<SqlParameter>();
            SqlParameter user = new SqlParameter( "@username", _username);
            SqlParameter password = new SqlParameter("@password", _password);                
            login.Add(user);
            login.Add(password);
            DataTable dt = new DataTable();
            dt = SendDataTableRequest("ChatLogin", login);

            if (dt.Rows.Count > 0)
                return true;
            else
                return false;
                              
        }

        //Method is used to add socket for room to list of room sockets
        public void addUserSocket(UserSocket userSocket)
        {
            _userSocket = userSocket;
        }

        //method is used to add active user to list
        public void addActiveUser(User user)
        {
            //verify user is not on list by checking username
            foreach (User u in _listActiveUsers)
            {
                if (u.UserName == user.UserName)
                {
                    //if user already in list, remove so fresh user can be added
                    _listActiveUsers.Remove(u);
                    break;
                }
            }
            //now user is unique so it can be added to list
            _listActiveUsers.Add(user);
        }

        
    }

}
